import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import Vant from 'vant'
import "vant/lib/index.css"
import '@/assets/config/less/style.less'
import Request from './utils/axios'
router.beforeEach((to,from,next) => {
  let token = localStorage.getItem('duokai_token')
  let bd_vid = sessionStorage.getItem('bd_vid') == undefined ? '' : sessionStorage.getItem('bd_vid')
  if (window.location.href.indexOf('bd_vid') != -1) {
    if (!bd_vid) {
      localStorage.removeItem('duokai_token')
      Request.get(`/launch?bdClickId=${window.location.href.split('=')[1]}`).then((res:any) => {
        if (res.data) {
          localStorage.setItem('duokai_token',res.data.token)
          localStorage.setItem('realUserId',res.data.realUserId)
          localStorage.setItem('userId',res.data.userId)
          next()
        }
      })
      sessionStorage.setItem('bd_vid',window.location.href.split('=')[1])
    } else {
      next()
    }
  } else if (token == undefined || !token) {
    Request.get(`/launch?bdClickId=${bd_vid}`).then((res:any) => {
      if (res.data) {
        localStorage.setItem('duokai_token',res.data.token)
        localStorage.setItem('realUserId',res.data.realUserId)
        localStorage.setItem('userId',res.data.userId)
        next()
      }
    })
  } else {
    next()
  }

  
})


const app = createApp(App);
app.use(Vant)
app.use(router);
app.use(createPinia());
app.mount('#app')
